﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KendoMvcUI.Core;
using KendoMvcUI.Common;
using System.Web.Mvc;
using KendoMvcUI.Enums;

namespace KendoMvcUI.UI
{
    public class Window:KendoUIBase
    {
        public Window(KendoUIControlSettings settings,ViewContext context,IViewDataContainer container)
            :base(settings,context,container)
        { }
        public override string RenderHtml()
        {
            return "<div id=\"{0}\"></div>".Formator(Settings.Id);
        }
    }
    public class WindowSettings : KendoUIControlSettings
    {
        public WindowSettings()
            : base("kendoWindow")
        {
            Actions = WindowActions.Minimize | WindowActions.Maximize | WindowActions.Close;
        }
        [PropertySetting(IsIngoreForJavaScript=true)]
        public Action<AnimationSettings> Animation { get; set; }
        /// <summary>
        /// The buttons for interacting with the window. 
        /// Predefined array values are "Close", "Refresh", "Minimize", and "Maximize".
        /// </summary>
        [PropertySetting(IsIngoreForJavaScript=true)]
        public WindowActions? Actions { get; set; }
        /// <summary>
        /// The element that the Window will be appended to. 
        /// Beneficial if the Window is used together with a form. 
        /// Note that this does not constrain the window dragging within the given element.
        /// </summary>
        public string AppendTo { get; set; }
        public bool? AutoFocus { get; set; }
        /// <summary>
        /// Specifies a URL or request options that the window should load its content from
        /// </summary>
        public string Content { get; set; }
        public bool? Draggable { get; set; }
        public bool? Iframe { get; set; }
        public int? MaxHeight { get; set; }
        public int? MaxWidth { get; set; }
        public int? MinHeight { get; set; }
        public int? MinWidth { get; set; }
        public bool? Modal { get; set; }
        public bool? Pinned { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [PropertySetting(IsIngoreForJavaScript = true)]
        public Action<PositionSettings> Position { get; set; }
        public bool? Resizable { get; set; }
        public string Title { get; set; }
        public bool? Visible { get; set; }
        public string Width { get; set; }
        public string Height { get; set; }
        #region 事件
        public string On_Activate { get; set; }
        public string On_Close { get; set; }
        public string On_Deactivate { get; set; }
        public string On_DragEnd { get; set; }
        public string On_DragStart { get; set; }
        public string On_Error { get; set; }
        public string On_Open { get; set; }
        public string On_Refresh { get; set; }
        public string On_Resize { get; set; }

        #endregion

        public override string ToJsSettings()
        {
            string jsScript = base.ToJsSettings();
            if (!jsScript.EndsWith(",")&&!jsScript.IsNullOrWhiteSpace())
                jsScript += ",";

            if (Animation != null)
            {
                AnimationSettings animation = new AnimationSettings();
                Animation(animation);
                jsScript += animation.ToString() + ",";
            }
            if(Actions.HasValue)
            {
                List<string> actionList = new List<string>();
                Actions.Value.ToString().Split(',').ToList().ForEach(a => {
                    actionList.Add("\"{0}\"".Formator(a.Trim()));
                });
                var actionsStr = string.Join(",",actionList);
                jsScript += "actions:[{0}],".Formator(actionsStr);
            }
            if (Position != null)
            {
                PositionSettings position = new PositionSettings();
                Position(position);

                jsScript += position.ToString()+",";
            }
            return jsScript.TrimEnd(',');
        }

        public class PositionSettings
        {
            public int? Top { get; set; }
            public int? Left { get; set; }

            public override string ToString()
            {
                string posStr = "";
                if (Top.HasValue)
                    posStr += "top:{0},".Formator(Top.Value);
                if (Left.HasValue)
                    posStr += "left:{0},".Formator(Left.Value);

                return "position:{" + posStr.TrimEnd(',') + "}";
            }
        }
    }
}
